package com.mxpio.erp.quality.policy;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.mxpio.erp.common.enums.InspectionEnums;
import com.mxpio.erp.common.enums.NoticeStatusEnums;
import com.mxpio.erp.common.inventory.entity.FeedPlanNotice;
import com.mxpio.erp.common.jpa.model.Notice;
import com.mxpio.erp.common.order.policy.AbstractNoticePolicy;
import com.mxpio.erp.common.order.policy.NoticeContext;
import com.mxpio.erp.common.purchase.entity.PurchaseOrder;
import com.mxpio.erp.common.quality.entity.*;
import com.mxpio.erp.common.sales.entity.SalesOrder;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.technology.entity.WorkShop;
import com.mxpio.erp.common.vo.OrderExcuteLineVo;
import com.mxpio.erp.common.workshop.entity.ManufactureOrder;
import com.mxpio.erp.common.workshop.entity.OutsourceOrder;
import com.mxpioframework.jpa.JpaUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

public class DefaultInQualityOrderNoticePolicy extends AbstractNoticePolicy {//入库类验收通知单(采购、生产、委外、销售退货)执行通用策略
	private List<OrderExcuteLineVo> orderExcuteVos;
	
	public DefaultInQualityOrderNoticePolicy(List<OrderExcuteLineVo> orderExcuteVos) {
		this.orderExcuteVos = orderExcuteVos;
	}
	
	@Override
	public void execute(NoticeContext context) {
		
	}

	@Override
	public void apply(NoticeContext context) {
		if(beforeExecute(context)) {
			execute(context);
			Notice notice = context.getEntity();
			List<OrderExcuteLineVo> orderExcuteVos = context.getOrderExcuteVos();
			Set<String> itemCodeSet = orderExcuteVos.stream().map(OrderExcuteLineVo::getItemCode).collect(Collectors.toSet());
			List<Item> items = JpaUtil.linq(Item.class).in("itemCode", itemCodeSet).list();
			Map<String, Item> itemMap = JpaUtil.index(items,"itemCode");//查询物料

			for(OrderExcuteLineVo vo : orderExcuteVos){
				InspectionBill bill = new InspectionBill();
				bill.setBusiType(InspectionEnums.ISBusiType.Stock.getCode());
				bill.setCheckStatus(InspectionEnums.CheckStatus.PLAN.getCode());//计划
				if (notice instanceof InQualityOrder){
					bill.setOriginBizType(InspectionEnums.OriginBizType.PURCH.getCode());
					String feedPlanNoticeNo = ((InQualityOrder) notice).getFeedPlanNoticeNo();
					String bizOrderNo = notice.getBizOrderNo();//bizOrderNo 可能为采购订单号，也可能为送货计划编号
					if (StringUtils.isBlank(feedPlanNoticeNo)){
						PurchaseOrder purchaseOrder = JpaUtil.getOne(PurchaseOrder.class, bizOrderNo);
						if (purchaseOrder!=null){
							bill.setApplyOrgType(InspectionEnums.ApplyOrgType.SUPPLY.getCode());
							bill.setApplyOrgCode(purchaseOrder.getPnCode());
							bill.setApplyOrgName(purchaseOrder.getPnName());
						}
					}else{//存在送货计划单号的情况
						FeedPlanNotice feedPlanNotice = JpaUtil.getOne(FeedPlanNotice.class, feedPlanNoticeNo);
						if (feedPlanNotice!=null){
							bill.setApplyOrgType(InspectionEnums.ApplyOrgType.SUPPLY.getCode());
							bill.setApplyOrgCode(feedPlanNotice.getPnCode());
							bill.setApplyOrgName(feedPlanNotice.getPnName());
						}
					}
				}else if (notice instanceof MoInQualityOrder){
					bill.setOriginBizType(InspectionEnums.OriginBizType.MANF.getCode());
					String bizOrderNo = notice.getBizOrderNo();
					ManufactureOrder manufactureOrder = JpaUtil.getOne(ManufactureOrder.class, bizOrderNo);
					if (manufactureOrder!=null){
						bill.setApplyOrgType(InspectionEnums.ApplyOrgType.WORKSHOP.getCode());
						bill.setApplyOrgCode(manufactureOrder.getMainWorkshop());
						WorkShop one = JpaUtil.getOne(WorkShop.class, manufactureOrder.getMainWorkshop());
						if (one!=null){
							bill.setApplyOrgName(one.getWorkShopName());
						}
					}
				}else if (notice instanceof OoInQualityOrder){
					bill.setOriginBizType(InspectionEnums.OriginBizType.OUTS.getCode());
					String bizOrderNo = notice.getBizOrderNo();
					OutsourceOrder outsourceOrder = JpaUtil.getOne(OutsourceOrder.class, bizOrderNo);
					if (outsourceOrder!=null){
						bill.setApplyOrgType(InspectionEnums.ApplyOrgType.PROC.getCode());
						bill.setApplyOrgCode(outsourceOrder.getPnCode());
						bill.setApplyOrgName(outsourceOrder.getPnName());
					}
				}else if (notice instanceof SoInQualityOrder){//销售退货
					bill.setOriginBizType(InspectionEnums.OriginBizType.SALERE.getCode());//销售退货入库验收
					String bizOrderNo = notice.getBizOrderNo();
					SalesOrder salesOrder = JpaUtil.getOne(SalesOrder.class, bizOrderNo);
					if (salesOrder!=null){
						bill.setApplyOrgType(InspectionEnums.ApplyOrgType.CUSTOMER.getCode());//客户
						bill.setApplyOrgCode(salesOrder.getPnCode());
						bill.setApplyOrgName(salesOrder.getPnName());
					}
				}
				bill.setOriginBizNo(vo.getBizOrderNo());
				bill.setOriginBizLineNo(vo.getBizOrderlineNo());
				bill.setOriginNoticeNo(vo.getNoticeNo());
				bill.setOriginNoticeLineNo(vo.getNoticeLineNo());
				String itemCode = vo.getItemCode();
				Item item = itemMap.get(itemCode);
				bill.setItemCode(itemCode);
				if (item!=null){
					bill.setItemName(item.getItemName());
					bill.setItemSpec(item.getItemSpec());
					bill.setUnitCode(item.getUnitCode());
					bill.setDrawingNo(item.getDrawingNo());
				}
				bill.setQuantity(vo.getQuantity());
				JpaUtil.save(bill);
			}
			afterExecute(context);
			notice.setNoticeStatus(NoticeStatusEnums.NoticeStatus.CONFIRM.getStatusCode());
			JpaUtil.update(notice);
		} else {
			if(CollectionUtils.isEmpty(context.getErrors())) {
				context.addError("前置校验不通过");
			}
		}

	}
	@Override
	public NoticeContext config(NoticeContext context) {
		context.setOrderExcuteVos(orderExcuteVos);
		return context;
	}

	@Override
	public boolean beforeExecute(NoticeContext context) {
		return true;
	}

	@Override
	public void afterExecute(NoticeContext context) {
		
	}
}
